package com.bbu.javaexam.controller;
/*
 * @ClassName QingLongApi$
 * @title QingLongApi
 * @description
 * @author 邓圣砚
 * @version 1.0
 * @create 2023/10/17 11:25
 */


import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.domain.UpdateJDCookieDto;
import com.bbu.javaexam.util.HttpClientUtil;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.web.bind.annotation.*;
import sun.net.www.http.HttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

@RestController
public class QingLongApi {


    private String authorizationToken = "Bearer ";

    @PostMapping("/updateJdCookie")
    public RespData updateJdCookie(@RequestBody UpdateJDCookieDto updateJDCookieDto) {
        String body = HttpUtil.get("http://117.50.197.47:5600/open/auth/token?client_id=oU2CxZ_4y46a&client_secret=gR5Ip7mrPQWhOsdF_rv8W3TU");
        JSONObject jsonObject = JSONUtil.parseObj(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String token = data.getStr("token");

        HashMap<String, Object> updateInfo = MapUtil.newHashMap();
        updateInfo.put("name","JD_COOKIE");
        updateInfo.put("remarks",null);
        updateInfo.put("value",updateJDCookieDto.getPtKey()+"pt_pin=%E4%B9%84%E6%A5%A0%E5%AE%9D;");
        updateInfo.put("id",1);

        String targetUrl = "http://117.50.197.47:5600/open/envs";
        authorizationToken += token;
        try {
            String resp = HttpClientUtil.sendHttpPut(targetUrl, authorizationToken, updateInfo);
            System.out.println("=============================="+resp);
            com.alibaba.fastjson.JSONObject jsonObject1 = com.alibaba.fastjson.JSONObject.parseObject(resp);
            if (jsonObject1.getInteger("code") == 200) {
                return RespDataHandler.success(jsonObject1);
            } else {
                return RespDataHandler.fail(jsonObject1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespDataHandler.success("更新成功");
    }

    @PostMapping("/getJdCookie")
    public RespData getJdCookie() {
        String targetUrl = "http://117.50.197.47:5600/open/envs";
        try {
            String resp = HttpClientUtil.sendHttpGetCookie(targetUrl, authorizationToken);
            System.out.println("=============================="+resp);
            com.alibaba.fastjson.JSONObject jsonObject1 = com.alibaba.fastjson.JSONObject.parseObject(resp);
            if (jsonObject1.getInteger("code") == 200) {
                return RespDataHandler.success(jsonObject1);
            } else {
                return RespDataHandler.fail(jsonObject1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespDataHandler.success("更新成功");
    }


//    public static void main(String[] args) {
//        HashMap<String, Object> headers = MapUtil.newHashMap();
//        headers.put("name","JD_COOKIE");
//        headers.put("remarks",null);
//        headers.put("value",updateJDCookieDto.getPtKey()+"pt_pin=%E4%B9%84%E6%A5%A0%E5%AE%9D;");
//        headers.put("id",1);
//    }
}
